/**
 * Created by Administrator on 2017/8/23 0023.
 */


var cellLayer;
var addCellLayer = false;
var cellItems =cells.items;

function addCellsLayerFun(){
    if(!addCellLayer){
        console.log("addLayer");
        var geojsonObject = {
            'type': 'FeatureCollection',
            'crs': {
                'type': 'name',
                'properties': {
                    'name': 'EPSG:4326'
                }
            },
            'features': [ {
                'type': 'Feature',
                'geometry': {
                    'type': 'Polygon',
                    'coordinates':[[[113.5082512010734,23.76968073642652],[113.6082512010734,23.86968073642652]
                    ]]
                }
            }]
        };
        if(cellItems){
            var radius = 0.2;
            var steps = 64;

            var angle = 30;
            for(i = 0,size = cellItems.length;i<size;i++){
                var start_angle = parseFloat(cellItems[i].AZIMUTH);
//            start_angle =0;
                var end_angle = start_angle + angle;
                var center = turf.point([changeMapLon(cellItems[i].LON),changeMapLat(cellItems[i].LAT)]);
                var sector = turf.sector(center, radius, start_angle, end_angle,steps);
                sector.properties.cell_name = cellItems[i].CELL_NAME;
//                for(var cnt =0;cnt <10;cnt++){
//                sector.properties ={
//                    color: 'green'
//                }
                if('819909-131' ==cellItems[i].CELL_ID ){
                    sector = turf.sector(center, 2, start_angle, end_angle,steps);
                    sector.properties.color='green'
                }
                geojsonObject.features.push(sector);
//                }
            }
            var source = new ol.source.Vector({
                features: (new ol.format.GeoJSON()).readFeatures(geojsonObject, {
                    dataProjection: "EPSG:4326",
                    featureProjection: "EPSG:3857"
                })
            });
            console.log("total cells "+geojsonObject.features.length);
            cellLayer = new ol.layer.Vector({
                source: source,
                style: styles
            });
            map.addLayer(cellLayer);
            map.getView().setCenter(qingyuan);
        }else{
            alert('no cells are found!');
        }
    }else{
        if(!!cellLayer){
            map.removeLayer(cellLayer);
        }
    }
    addCellLayer = !addCellLayer;
}